from lxml import etree
import requests
import os
import time

os.mkdir('./beautiful')
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36'
}
num = int(input('how many?'))
for each_page in range(2,num+1):
    url = 'http://pic.netbian.com/4kmeinv/index_%d.html' % each_page
    page_text = requests.get(url,headers).text
    print(page_text)
    tree = etree.HTML(page_text)
    li_list = tree.xpath('//ul[@class="clearfix"]/li')
    print(li_list)
    for each_li in li_list:
        pic_url = 'http://pic.netbian.com' + each_li.xpath('./a/img/@src')[0]
        pic_name = pic_url.split('/')[-1]
        pic_path = './beautiful/' + pic_name
        response = requests.get(url=pic_url,headers=headers).content
        with open(pic_path,'wb') as f:
            f.write(response)
        print(pic_name,"爬取成功")
        time.sleep(1)
print('爬取完成~~~~')
